package com.ivanceras.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Questionnaire implements Serializable{

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    public Long questionnaireID;
    
    @Persistent
	public String title;

    @Persistent
    public String description;

    @Persistent 
    public int items; //this is for storing the item count to pre-empt initialization of UI components and layouts

    @Persistent 
    public int[] choices; //this is for storing the number of items for each choices when initializing the questionnaire
    
	@NotPersistent
    public List<Question> questions  = new ArrayList<Question>();

	public Questionnaire(){
    }
    public Questionnaire(String title, String description){
    	this.title = title;
    	this.description = description;
    }
	public void addQuestion(Question question){
		question.questionnaireID = this.questionnaireID; //questionnaire ID is defined already
		questions.add(question);
    	items = questions.size();
    	choices = new int[items]; //recreate the array
    	for(int i = 0; i < items; i++){
    		Question qn = questions.get(i);
	    	choices[i] = qn.choiceList.size();
    	}
	}
}
